﻿using System;
using System.Xml;
using System.Text;
using System.Collections.Generic;

namespace SBPweb.PhotoGallery.Host
{

    public class Slides : List<Slide>, ICloneable
    {

        #region Private fields

        private string pBasePath = "Gallery/";
        private int? pPageIndex;
        private int? pPageSize;

        #endregion

        #region Constructor logic

        public Slides()
            : this(string.Empty)
        {
        }

        public Slides(string basePath)
        {
            if (!string.IsNullOrEmpty(basePath))
            {
                pBasePath = basePath;
            }
        }

        public Slides(XmlDocument xml)
            : this(string.Empty)
        {
            XmlNode rootNode = xml.DocumentElement;
            XmlAttribute xaBasePath = rootNode.Attributes["baseUrl"];
            XmlAttribute xaPageIndex = rootNode.Attributes["page"];
            XmlAttribute xaPageSize = rootNode.Attributes["pageSize"];
            if (xaBasePath != null)
            {
                BasePath = xaBasePath.Value;
            }
            if (xaPageIndex != null)
            {
                PageIndex = Convert.ToInt32(xaPageIndex.Value);
            }
            if (xaPageSize != null)
            {
                PageSize = Convert.ToInt32(xaPageSize.Value);
            }

            foreach (XmlNode node in rootNode.ChildNodes)
            {
                this.Add(new Slide(node));
            }
        }

        #endregion

        #region Property accessors

        public string BasePath
        {
            get
            {
                return pBasePath;
            }
            set
            {
                pBasePath = value;
            }
        }

        public int? PageIndex
        {
            get
            {
                return pPageIndex;
            }
            set
            {
                pPageIndex = value;
            }
        }

        public int? PageSize
        {
            get
            {
                return pPageSize;
            }
            set
            {
                pPageSize = value;
            }
        }

        #endregion

        #region Methods

        public object Clone()
        {
            return Clone(0, this.Count);
        }

        public Slides Clone(int index, int count)
        {
            Slides slides = new Slides(BasePath);
            slides.AddRange(this.GetRange(index, count));
            return slides;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sb.AppendFormat("<slides baseUrl=\"{0}\"", BasePath);
            if (PageIndex.HasValue)
            {
                sb.AppendFormat(" page=\"{0}\"", PageIndex.Value);
            }
            if (PageSize.HasValue)
            {
                sb.AppendFormat(" pageSize=\"{0}\"", PageSize.Value);
            }
            sb.AppendLine(">");
            foreach (Slide slide in this)
            {
                sb.AppendLine(slide.ToString());
            }
            sb.Append("</slides>");
            return sb.ToString();
        }

        #endregion

    }

}